package com.mall.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.common.constant.MessageStatusEnum;
import com.mall.system.entity.SmsLog;
import com.mall.system.mapper.SmsLogMapper;
import com.mall.system.service.SmsLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 短信发送日志服务实现类
 */
@Slf4j
@Service
public class SmsLogServiceImpl extends ServiceImpl<SmsLogMapper, SmsLog> implements SmsLogService {

    @Override
    public Long saveSmsLog(String mobile, String content, Integer status, String errorMessage) {
        try {
            SmsLog smsLog = new SmsLog();
            smsLog.setMobile(mobile);
            smsLog.setContent(content);
            smsLog.setStatus(status);
            smsLog.setErrorMessage(errorMessage);
            smsLog.setCreateTime(LocalDateTime.now());
            save(smsLog);
            return smsLog.getLogId();
        } catch (Exception e) {
            log.error("保存短信发送日志失败: {}", e.getMessage());
            return null;
        }
    }
    
    @Override
    public boolean updateSmsLogStatus(Long logId, Integer status, String errorMessage) {
        try {
            SmsLog smsLog = getById(logId);
            if (smsLog != null) {
                smsLog.setStatus(status);
                smsLog.setErrorMessage(errorMessage);
                return updateById(smsLog);
            }
            return false;
        } catch (Exception e) {
            log.error("更新短信发送状态失败: {}", e.getMessage());
            return false;
        }
    }
}